package cn.support.neo.mvp.ui.view.imgcompress;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.neo.support.img.KCompressor;
import cn.neo.support.img.OnMultiCompressListener;
import cn.support.neo.mvp.R;
import cn.support.neo.mvp.core.base.BaseFragment;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * <b>Name:</b>xx页面<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b>  2017-05-09<br>
 * <b>Describe:</b><p> PLEASE TO FIXME...</p><br>
 * <p>代码说明：（重点）<br>
 * <ol>
 * <li> write something content..</li>
 * </ol>
 * </p>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b>
 * <ul>
 * v1.x<li>1.write something content..。<br>2.write something content..<br>3.write something content..<br></li>
 * </ul>
 * <br>
 */
public class ImageCompressFM extends BaseFragment {
    private static final String TAG = "KCompressTest";
    private static final int REQUEST_CODE = 1;

    private List<ImageView> mImageViews;
    private List<File> mFileList;

    @Override
    protected int getLayoutResID() {
        return R.layout.fm_img_compress;
    }

    @Override
    protected void initView() {
        mFileList = new ArrayList<>();
        mImageViews = new ArrayList<>();
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_1));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_2));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_3));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_4));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_5));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_6));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_7));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_8));
        mImageViews.add((ImageView) getActivity().findViewById(R.id.image_9));
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void lazyLoadingTask() {

    }

    /**
     * 选择照片
     */
    public void onClick4Select(View view) {
        MultiImageSelector.create().start(this, REQUEST_CODE);
    }

    /**
     * 压缩图片
     * @param view
     */
    public void onClick4Compress(View view) {
        KCompressor.with(getContext())
                .setMaxSize(150)
                .setTarget((File[]) mFileList.toArray())
                .setCacheDir(getContext().getExternalCacheDir())
                .setListener(new OnMultiCompressListener() {
                    @Override
                    public void onStart() {
                        Log.i(TAG, "start");
                    }

                    @Override
                    public void onSuccess(List<File> fileList) {
                        int size = fileList.size();
                        while (size-- > 0) {
                            mImageViews.get(size).setImageURI(Uri.fromFile(fileList.get(size)));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }
                })
                .launch();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && data != null) {
            mFileList.clear();
            List<String> path = data
                    .getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            for (String str : path) {
                mFileList.add(new File(str));
            }
        }

    }
}
